<?php

/**
 * 自动生成 MerUserAddressService
 * 
 * Apps/Common/Service/MerUserAddressService.class.php
 * ryadmin @ 2015-06-29
 */
namespace Common\Service;

class MerUserAddressService {
  private static $instance;
  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerUserAddressService ();
    }
    
    return $instance;
  }
  function get_default_row() {
    return array (
        'id' => '',
        'mer_user' => '',
        'area' => '',
        'address' => '',
        'is_default' => '0',
        'create_date' => date ( 'Y-m-d H:i:s' ),
        'postal_code' => '',
        'phone' => '',
        'expand' => '' 
    );
  }
  function get_by_id($id) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $data = $MerUserAddress->find ( $id );
    return $data ? $data : array ();
  }
  function get_by_cond($config) {
    $default = array (
        'page' => 1,
        'page_size' => 6,
        'status' => '',
        'count' => FALSE,
        'order' => 'DESC',
        'sort' => 'id',
        '' 
    );
    
    $config = extend ( $config, $default );
    
    $MerUserAddress = M ( 'MerUserAddress' );
    
    $where = array ();
    
    if (! empty ( $config ['status'] )) {
      $where ['status'] = $config ['status'];
    }
    $where ["mer_user"] = $config ["mer_user"];
    if ($config ['count']) {
      return $MerUserAddress->where ( $where )->count ();
    } else {
      $order = $config ['sort'] . ' ' . $config ['order'];
      $limit = ($config ['page'] - 1) * $config ['page_size'] . ' , ' . $config ['page_size'];
      $data = $MerUserAddress->where ( $where )->limit ( $limit )->order ( $order )->select ();
      // echo $MerUserAddress->_sql();
      return $data ? $data : array ();
    }
  }
  function get_address($mer_user) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $where = array ();
    $where ["mer_user"] = $mer_user;
    $data = $MerUserAddress->where ( $where )->select ();
    return $data ? $data : array ();
  }
  function create($data, $is_ajax = true) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $ret = $MerUserAddress->add ( $data );
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr ( '添加成功', TRUE, array (
            'id' => $ret 
        ) );
      } else {
        return ajax_arr ( '添加失败', FALSE );
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }
  function update($id, $data) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $ret = $MerUserAddress->where ( "id = %d", $id )->save ( $data );
    if ($ret) {
      return ajax_arr ( '编辑成功', TRUE );
    } else {
      return ajax_arr ( '编辑失败', FALSE );
    }
  }
  function set_default($mer_user, $id) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $ret = $MerUserAddress->where ( "mer_user = %d", $mer_user )->data ( array (
        'is_default' => array (
            'exp',
            'if(id=' . $id . ',1,0)' 
        ) 
    ) )->save ();
    if ($ret) {
      return ajax_arr ( '编辑成功', TRUE );
    } else {
      return ajax_arr ( '编辑失败', FALSE );
    }
  }
  function set_default_address($mer_user) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $where = array ();
    $where ["mer_user"] = $mer_user;
    $MerUserAddress->where ( $where )->order ( "create_date desc" )->limit ( 1 )->setField ( "is_default", 1 );
    if ($ret) {
      return ajax_arr ( '编辑成功', TRUE );
    } else {
      return ajax_arr ( '编辑失败', FALSE );
    }
  }
  function get_address_count($mer_user) {
    $MerUserAddress = M ( 'MerUserAddress' );
    return $MerUserAddress->where ( "mer_user = %d", $mer_user )->count ();
  }
  function has_default_address($mer_user) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $where = array ();
    $where ["mer_user"] = $mer_user;
    $where ["is_default"] = 1;
    return $MerUserAddress->where ( $where )->count () > 0;
  }
  function delete($mer_user, $ids) {
    $MerUserAddress = M ( 'MerUserAddress' );
    $ret = $MerUserAddress->where ( "mer_user = %d", $mer_user )->delete ( $ids );
    if ($ret == 0) {
      return ajax_arr ( '未删除任何数据', FALSE );
    } else if (! $ret) {
      return ajax_arr ( '删除失败', FALSE );
    } else {
      return ajax_arr ( '删除' . $ret . '行数据', TRUE );
    }
  }
}
